package com.macaline.util;

import java.util.concurrent.*;

public class ThreadUtil {

    private static ExecutorService pool = null;


    /**
     * 执行线程任务
     * @param runnable
     */
    public static void execute(Runnable runnable){
        getPool().execute(runnable);
    }

    /**
     * 关闭线程
     */
    public static void shutdown(){
        getPool().shutdown();
    }


    /**
     * 获取线程池
     * @return
     */
    public static ExecutorService getPool() {
        if (pool == null){
            Object o = new Object();
            synchronized (o){
                if(pool == null){
                    pool = new ThreadPoolExecutor(
                            30,
                            50,
                            200L, TimeUnit.SECONDS,
                            new ArrayBlockingQueue<>(300),
                            Executors.defaultThreadFactory(),
                            new ThreadPoolExecutor.DiscardOldestPolicy());
                }
            }
        }
        return pool;
    }

}
